﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CondominioG1.Negocio;
using CondominioG1.Dominio;

namespace CondominioG1.Controllers
{
    public class ResidenteController : Controller
    {
        //Dependencias
        public ResidenteService ResidenteService { get; set; }
                 
        // GET: /Residente/
        public ActionResult Index()
        {
            ICollection<Residente> modelo = ResidenteService.ListarTodosLosResidentes();
            return View(modelo);
        }


        
         //GET: /Residente/
        public ActionResult Lista(String Dominio)
        {
            ICollection<Residente> modelo = ResidenteService.ListarResidentePorCorreo(Dominio);
            return View(modelo);
        }


        // GET: /Residente/Details/5
        public ActionResult Details(String Codigo)
        {
            Residente modelo = ResidenteService.ObtenerResidente(Codigo);
            return View(modelo);
        }

        // GET: /Residente/Create
        public ActionResult Create()
        {
     
          return View();
        } 

        // POST: /Residente/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {

                if (ModelState.IsValid) 
                {
                    ResidenteService.RegistrarResidente((String)collection["Dni"],
                                                    (String)collection["Nombres"],
                                                    (String)collection["Apellidos"],
                                                    Int32.Parse(collection["Edad"]),
                                                    (String)collection["Correo"],
                                                    (String)collection["Clave"],
                                                    (String)collection["ctl00$MainContent$cmbTipo"]
                                                    );
                    return RedirectToAction("Index");
                }

                else
                {
                    return View();
                }
              
            }
            catch (Exception e)
            {

                ModelState.AddModelError(String.Empty, "Error al crear al residente: " + e.Message);
                return View();
            }
        }
        
        // GET: /Residente/Edit/5
        public ActionResult Edit(String Codigo)
        {
            Residente modelo = ResidenteService.ObtenerResidente(Codigo);
            return View(modelo);
        }

        // POST: /Residente/Edit/5
        [HttpPost]
        public ActionResult Edit(String Codigo, FormCollection collection)
        {
            try
            {
                ResidenteService.ModificarResidente((String)collection["Dni"],
                                                    (String)collection["Nombres"],
                                                    (String)collection["Apellidos"],
                                                    Int32.Parse(collection["Edad"]),
                                                    (String)collection["Correo"],
                                                    (String)collection["Clave"],
                                                    (String)collection["Tipo"]
                                                    );
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: /Residente/Delete/5
        public ActionResult Delete(String Codigo)
        {
               Residente modelo = ResidenteService.ObtenerResidente(Codigo);
               return View(modelo);
        }

        //
        // POST: /Residente/Delete/5

        [HttpPost]
        public ActionResult Delete(String Codigo, FormCollection collection)
        {
            try
            {
                ResidenteService.EliminarResidente(Codigo);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}

             
